<?php
namespace haibao\cms\business\site;
use haibao\cms\business\BusinessException;
use haibao\cms\data\site\PageTemplate as PageTemplateData;
use \haibao\cms\model\filter\site\PageTemplate as PageTemplateFilter;
use \haibao\cms\model\filter\site\SiteChannelTemplate as SiteChannelTmpFilter;
use haibao\frame\data\query\Condition;

class PageTemplate extends \haibao\cms\business\Base{

	/**
	 * 添加页面模版
	 * @param : Object \haibao\cms\model\data\site\PageTemplate $pageTemplateModel
	 */
	public static function addPageTemplate($pageTemplateModel){

		self::checkOperation(self::FUNC_CODE_PAGE_TEMPLATE_ADD);
		if(!$pageTemplateModel->TemplateName){
			throw new BusinessException('模版名不能为空');
		}		
		if(!$pageTemplateModel->Content){
			throw new BusinessException('模版内容不能为空');
		}

		$pageTemplateData = new PageTemplateData();
		$pageTemplateData->addPageTemplate($pageTemplateModel);
		self::saveOperateHistory(self::FUNC_CODE_PAGE_TEMPLATE_ADD, $pageTemplateModel->toArray(true,true));
		
	}
	
	/**
	 * 修改页面模版
	 * @param : Object \haibao\cms\model\data\site\PageTemplate $pageTemplateModel
	 */
	public static function editPageTemplate($pageTemplateModel){
	
		self::checkOperation(self::FUNC_CODE_PAGE_TEMPLATE_EDIT);
		if(!$pageTemplateModel->TemplateName){
			throw new BusinessException('模版名不能为空');
		}
		if(!$pageTemplateModel->Content){
			throw new BusinessException('模版内容不能为空');
		}
		
		$pageTemplateData = new PageTemplateData();
		$pageTemplateData->updatePageTemplate($pageTemplateModel);
		self::saveOperateHistory(self::FUNC_CODE_PAGE_TEMPLATE_EDIT, $pageTemplateModel->toArray(true,true));
		
	}
	
	/**
	 * 根据ID获取页面模版信息
	 */
	public static function getPageTemplateById($templateId){
		
		if(!$templateId){
			throw new BusinessException('模版ID不能为空');
		}
		$pageTemplateData = new PageTemplateData();
		return $pageTemplateData->getOneById($templateId);
	}
	
	/**
	 * 根据filter 获取页面模版信息
	 * @param : Object \haibao\cms\model\filter\site\PageTemplate $filter
	 * @param : Int $isOne
	 */
	public static function getPageTemplateByFilter($filter,$isOne = true){

		$pageTemplateData = new PageTemplateData();
		if($isOne){
			$pageTemplateInfo = $pageTemplateData->getOne($filter);
		}else{

			$pagetemplateinfo = $pageTemplateData->getAll($filter);
		}
		return $pagetemplateinfo; 
	}
	
	/**
	 * 删除页面模版
	 * @param : Int $id
	 * @param : Int $status
	 */
	public static function deletePageTmpById($id, $status = \haibao\cms\model\data\site\SiteChannelTemplate::STATUS_ENABLE){

		self::checkOperation(self::FUNC_CODE_PAGE_TEMPLATE_DEL);
		$data = new PageTemplateData();
		$data->deletePageTmpById($id,$status);
		self::saveOperateHistory(self::FUNC_CODE_PAGE_TEMPLATE_DEL, array($id,$status));
	}
	
	/**
	 * 根据模版名字获取模版信息
	 * @param : String $templateName
	 * @param : Int $templateId
	 */
	public static function getPageTmpByTmpName($templateName, $templateId = null){

		$data = new PageTemplateData();
		return $data->getPageTmpByTmpName($templateName,$templateId);
	}
	
	/**
	 * 判断模版使用被频道使用
	 * @param : Int $id 模版ID
	 */
	public static function whetherUseForTemplate($id){
		$data = new PageTemplateData();
		return $data->whetherUseForTemplate($id);
	}
	/**
	 * 更新模版静态缓存
	 * @param : Int $id
	 */
	public static function updateTemplateCacheById($id){

		$data = new PageTemplateData();
		$arrSet = 
			array(
				\haibao\cms\model\filter\site\PageTemplate::CONDITION_FIELD_NAME_UPDATE_TIME => new \DateTime(),
		);
		$data->updateById($id, $arrSet);
	}
	
	/**
	 * 根据模版名字获取站点与模版的关联信息
	 */
	public static function getSiteChannelTmpByTemplateName($templateName){
		
		if(!$templateName){
			throw new BusinessException('模版名不能为空');
		}
		
		$pageTemplateFilter = new PageTemplateFilter();
		$pageTemplateFilter->select(array(
				PageTemplateFilter::CONDITION_FIELD_NAME_TEMPLATE_NAME,
				PageTemplateFilter::CONDITION_FIELD_NAME_CONTENT,
				PageTemplateFilter::CONDITION_FIELD_NAME_UPDATE_TIME,
				PageTemplateFilter::CONDITION_FIELD_NAME_TEMPLATE_PATH,
		));
		$siteChannelTmpFilter = new SiteChannelTmpFilter();
		$siteChannelTmpFilter->select(array(
				SiteChannelTmpFilter::CONDITION_FIELD_NAME_ID,
				SiteChannelTmpFilter::CONDITION_FIELD_NAME_CHANNEL_ID,
		));
		$pageTemplateFilter->where(PageTemplateFilter::CONDITION_FIELD_NAME_TEMPLATE_NAME,Condition::CONDITION_EQUAL,$templateName);
		
		$pageTemplateFilter->leftJoin($siteChannelTmpFilter,null,PageTemplateFilter::CONDITION_FIELD_NAME_ID,SiteChannelTmpFilter::CONDITION_FIELD_NAME_TEMPLATE_ID,PageTemplateFilter::CONDITION_FIELD_NAME_SITE_CHANNEL_TMP,false);
		$pageTemplateData = new PageTemplateData();
		$pageTemplate = $pageTemplateData->getOne($pageTemplateFilter);
		if($pageTemplate->TemplatePath){
			$contentPathSVN = \haibao\classlibrary\Config::getConfig(\haibao\classlibrary\Config::SITE_PAGE_TEMPLATE_PATH).$pageTemplate->TemplatePath;
			if(file_exists($contentPathSVN)){
				$pageTemplate->Content = file_get_contents($contentPathSVN);
			}
		}
		return $pageTemplate;
	}
}